package cn.com.xy.sms.sdk.db.entity;

import java.util.Map;

import org.json.JSONObject;

import android.content.ContentValues;
import cn.com.xy.sms.sdk.constant.Constant;
import cn.com.xy.sms.sdk.db.DBManager;
import cn.com.xy.sms.sdk.db.XyCursor;
import cn.com.xy.sms.sdk.db.base.BaseManager;
import cn.com.xy.sms.sdk.log.LogManager;

public class OperatorCmdManager {

    public static final String TABLE_NAME = "tb_operator_cmd_info";
    
    public static final String ID = "id";
    
    public static final String PHONE = "phone";
    
    public static final String ICCID = "iccid";
    
    public static final String ACTIONS = "actions";
    
    public static final String UPDATE_INFO_TIME = "updateInfoTime";
    
    private static String[] mColumns = new String[] {UPDATE_INFO_TIME,ACTIONS};


    public static String getCreateTableSql() {
        return " create table  if not exists " + TABLE_NAME + " ("
                + ID+"  INTEGER PRIMARY KEY,"           
                + PHONE +"  TEXT," + ICCID +"  TEXT," + ACTIONS +" TEXT,"           
                + UPDATE_INFO_TIME+"  long DEFAULT '0')";
    }
    
    
    public static JSONObject queryOperatorCmd(String phoneNum, String simIccid,Map extend){
    	JSONObject jsonObject = null;
          XyCursor cur = null;
          try {
              cur = DBManager.query(TABLE_NAME,mColumns, PHONE + " = ?" , new String[] { phoneNum}, null, null,
                      null, null);
              jsonObject = BaseManager.loadSingleDataFromCursor(mColumns, cur);
          } catch (Throwable e) {
//              e.printStackTrace();
              /*SDK-550 hewengao 20160719 start*/
              LogManager.w(Constant.TAG, "queryOperatorCmd: ", e);
              /*SDK-550 hewengao 20160719 end*/
          } finally {
              XyCursor.closeCursor(cur, true);
          }
          return jsonObject;
    }
    
    public static long insertOrUpdateOperatorCmd(String phoneNum, String simIccid,String actions) {
        long result = -1;
        ContentValues values = null;
        try {
            values = BaseManager.getContentValues(null, PHONE, phoneNum,
            		ICCID, simIccid, ACTIONS,actions,UPDATE_INFO_TIME,
                    String.valueOf(System.currentTimeMillis()));
            // 更新点击次数     /*HWWATCH-4 songzhirong 20160818 start*/
            int rows = DBManager.update(TABLE_NAME, values, PHONE
                    + " = ? or " + ICCID + " = ?", new String[] {
                    phoneNum, simIccid });
            /*HWWATCH-4 songzhirong 20160818 end*/
            if (rows <= 0) {
                result = DBManager.insert(TABLE_NAME, values);
            } else {
                result = 0;
            }
        } catch (Throwable e) {
//            e.printStackTrace();
            /*SDK-550 hewengao 20160719 start*/
            LogManager.w(Constant.TAG, "insertOrUpdateOperatorCmd: ", e);
            /*SDK-550 hewengao 20160719 end*/
        } finally {
        }
        return result;
    }

    
}
